﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.Data;
using System.Linq;
using System.ServiceModel.DomainServices.EntityFramework;
using System.ServiceModel.DomainServices.Hosting;
using System.ServiceModel.DomainServices.Server;
using MineSolutions.ExtendedQueryableFilter.WebDomainApplication.Model;

namespace MineSolutions.ExtendedQueryableFilter.WebDomainApplication.Service
{
    [EnableClientAccess()]
    public class MainDomainService : LinqToEntitiesDomainService<AdventureWorksEntities>
    {
        [Query(IsDefault = true)]
        public IQueryable<Employee> GetEmployees()
        {
            return this.ObjectContext.Employees.OrderBy(e => e.EmployeeID);
        }

        public void InsertEmployee(Employee employee)
        {
            if ((employee.EntityState != EntityState.Detached))
            {
                this.ObjectContext.ObjectStateManager.ChangeObjectState(employee, EntityState.Added);
            }
            else
            {
                this.ObjectContext.Employees.AddObject(employee);
            }
        }

        public void UpdateEmployee(Employee currentEmployee)
        {
            this.ObjectContext.Employees.AttachAsModified(currentEmployee, this.ChangeSet.GetOriginal(currentEmployee));
        }

        public void DeleteEmployee(Employee employee)
        {
            if ((employee.EntityState == EntityState.Detached))
            {
                this.ObjectContext.Employees.Attach(employee);
            }
            this.ObjectContext.Employees.DeleteObject(employee);
        }
    }
}